#include "recvtablemodel.h"
#include "commonhelper.h"
#include <QDebug>

RecvTableModel::RecvTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{

    m_headList<<"商品编号"<<"条码"<<"全称"<<"简称"
              <<"包装数量"<<"单位"<<"种类"
              <<"售价"<<"进价"<<"增加日期";
//    QStringList dataList;
//    dataList<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234";
//    m_dataList.append(dataList);
//    m_dataList.append(dataList);
//    m_dataList.append(dataList);
//    m_dataList.append(dataList);
}

QVariant RecvTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    // FIXME: Implement me!
    if(role != Qt::DisplayRole)//添加此判断解决水平表头不能显示的问题
        return QVariant();

    if(orientation == Qt::Vertical) {
        return QVariant(section+1);
    }
    QVariant value;
    if(section >= 0 && section < m_headList.size() && orientation == Qt::Horizontal)
        value = m_headList.at(section);
    return value;
}

int RecvTableModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    // FIXME: Implement me!
    return m_dataList.size();
}

int RecvTableModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    // FIXME: Implement me!
    return m_headList.count();
}

QVariant RecvTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    QVariant value;
    if(role == Qt::DisplayRole)
    {
        const int row = index.row();
        const int column = index.column();
        if(row >= 0 && row < m_dataList.size())
        {
            if(column >= 0 && column < m_dataList[row].size())
               value = m_dataList[row].at(column);
            else
               value = "";
        }
    }
    return value;
}

//设置模型数据
void RecvTableModel::setTableData(const QList<QStringList> &dataList)
{
    beginResetModel();
    m_dataList.clear();
    m_dataList = dataList;
    endResetModel();
}

//删除一行商品信息(返回编号)
QString RecvTableModel::removeCurrentRowData(int row)
{
    if(row < 0){
        return QString("");
    }
    QString goodId = m_dataList[row].at(0);
    beginResetModel();
    m_dataList.removeAt(row);
    endResetModel();
    return goodId;
}

void RecvTableModel::clearData()
{
    beginResetModel();
    m_dataList.clear();
    endResetModel();
}

QString RecvTableModel::getBarCode(int row)
{
    if(row < 0){
        return QString("");
    }
    return m_dataList[row].at(1);
}

QString RecvTableModel::getGoodId(int row)
{
    if(row < 0){
        return QString("");
    }
    return m_dataList[row].at(0);
}

void RecvTableModel::insertOneRow(QStringList &infoList, QString barCode)
{

    QStringList barCodeList = barCode.split(";");
    beginResetModel();
    foreach (const QString &code, barCodeList) {
        QStringList goodList = infoList;
        goodList.insert(1, code);
        m_dataList.push_back(goodList);
    }
    endResetModel();
}

void RecvTableModel::appendRow(QStringList &infoList)
{
    beginResetModel();
    m_dataList.append(infoList);
    endResetModel();
}

QStringList RecvTableModel::getData(int row)
{
    if(row  < 0)
        return QStringList();
    return m_dataList.at(row);
}

bool RecvTableModel::updateOneRow(QStringList &infoList, QString barCode, int row)
{
    if(row < 0)
        return false;
    infoList.insert(1, barCode);
    beginResetModel();
    m_dataList[row] = infoList;
    endResetModel();
    return true;
}


